package com.example.wordpress;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.util.Log;

/**
 * Entrada de blog.
 * @author Ignacio Javier Kovacs
 *
 */

public class Post
{
	public static final String WORDPRESS_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; /* ISO_8601 */
	public static final String APPLICATION_DATE_FORMAT = "dd'/'MM'/'yyyy hh':'mm a";
	
	private int _id;
	private int blog_id;
	private Date date;
	private Date modified;
	private String title;
	private String url;
	private String short_url;
	private String content;
	private String excerpt;
	private String status;
	private String type;
	private boolean comments_open;
	private boolean pings_open;
	private String format;
	
	private List<String> image_urls;
	
	public Post() {
		image_urls = new ArrayList<String>();
	}
	
	/* Getter/Setter */
	
	public int getId() {
		return _id;
	}
	
	public void setId(int _id) {
		this._id = _id;
	}
	
	public int getBlogId() {
		return blog_id;
	}
	
	public void setBlogId(int blog_id) {
		this.blog_id = blog_id;
	}

	public String getVisualDate() {
		return convertDateToString(date, APPLICATION_DATE_FORMAT);
	}
	
	public String getDate() {
		return convertDateToString(date, WORDPRESS_DATE_FORMAT);
	}
	
	public Date getRawDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = convertStringToDate(date, WORDPRESS_DATE_FORMAT);
	}
	
	public String getVisualModified() {
		return convertDateToString(modified, APPLICATION_DATE_FORMAT);
	}

	public String getModified() {
		return convertDateToString(modified, WORDPRESS_DATE_FORMAT);
	}
	
	public Date getRawModified() {
		return modified;
	}

	public void setModified(String modified) {
		this.modified = convertStringToDate(modified, WORDPRESS_DATE_FORMAT);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getShortUrl() {
		return short_url;
	}

	public void setShortUrl(String short_url) {
		this.short_url = short_url;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getExcerpt() {
		return excerpt;
	}

	public void setExcerpt(String excerpt) {
		this.excerpt = excerpt;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean isCommentsOpen() {
		return comments_open;
	}

	public void setCommentsOpen(boolean comments_open) {
		this.comments_open = comments_open;
	}

	public boolean isPingsOpen() {
		return pings_open;
	}

	public void setPingsOpen(boolean pings_open) {
		this.pings_open = pings_open;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}
	
	public List<String> getImageUrls() {
		return image_urls;
	}

	public void putImageUrl(String url) {
		image_urls.add(url);
	}

	/**
	 * Convierte de String a Date usando el formato especificado.
	 * @param date_string fecha a convertir.
	 * @param inputFormat formato de entrada.
	 * @return Date, null si falla.
	 */
	private Date convertStringToDate(String date_string, String inputFormat) {
		Date date = null;
		SimpleDateFormat input = new SimpleDateFormat(inputFormat, Locale.getDefault());
		try {
			date = input.parse(date_string);
		}
		catch(ParseException e) {
			Log.e("[ParseException]", e.getMessage());
		}
		return date;
	}
	
	/**
	 * Convierte de Date a String usando el formato especificado.
	 * @param date fecha a convertir.
	 * @param outputFormat formato de salida.
	 * @return String
	 */
	private String convertDateToString(Date date, String outputFormat) {
		SimpleDateFormat ouput = new SimpleDateFormat(outputFormat, Locale.getDefault());
		return ouput.format(date);
	}
}
